﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;

namespace Fatec.ControleEstoque.Interface.DynamicData.FieldTemplates
{
    public partial class MaskText_Edit : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.ToolTip = Column.Description;
            TextBox1.MaxLength = Column.MaxLength;

            var dataType = Column.Attributes.OfType<DataTypeAttribute>().FirstOrDefault();
            switch (dataType.DataType)
            {
                case DataType.Custom:
                    switch (dataType.CustomDataType )
                    {
                        case "CNPJ":
                            TextBox1_MaskedEditExtender.Mask = @"99\.999\.999\/9999\-99";
                            break;
                        case "CEP":
                            TextBox1_MaskedEditExtender.Mask = @"99999\-999";
                            break;
                    }

                    break;

                case DataType.PhoneNumber:
                    TextBox1_MaskedEditExtender.Mask = @"99\-9999-9999";
                    break;
                default:
                    break;
            }

            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(TextBox1.Text);
        }

        public override Control DataControl
        {
            get
            {
                return TextBox1;
            }
        }
    }
}